package com.zyb.interceptor;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{
	
	private List<String> url = new ArrayList();

	/**
	 * 开始进入地址请求拦截
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		if(session.getAttribute("user") != null){
			return true;
		}else{
			response.sendRedirect("/");	//未登录，跳转到登录页
			return false;
		}
	}

	/**
	 * 处理请求完成后视图渲染之前的处理操作
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	/**
	 * 视图渲染之后的操作
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		 
	}
	

	public List<String> getUrl(){
		url.add("/");      //登录页
        //网站静态资源
        url.add("/css/**");
        url.add("/js/**");
        url.add("/fonts/**");
        url.add("/images/**");
        url.add("/login"); //配置登陆拦截，这是登陆的请求页面，也不应该进行拦截
		return url;
	}	

}
